/*
 * This file is part of the openHiTLS project.
 *
 * openHiTLS is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "hitls_build.h"
#ifdef HITLS_CRYPTO_SM9

#include "sm9_curve.h"

#define BN256IN(a, b, c, d, e, f, g, h)        {h, g, f, e, d, c, b, a}

SM9_CTX g_sm9_ctx;

// SM9 para
SM9_Sys_Para sm9_sys_para = {
    /* Algorithem Word Length */
    8,

    /* Parameter Trace
    t:            600000000058F98A
    6*t+2:        2400000000215D93E
    trace:        D8000000019062ED0000B98B0CB27659                                                            */
    BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x60000000, 0x0058F98A),//EC_T
    BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x40000000, 0x0215D93E),//EC_6T2
    BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xD8000000, 0x019062ED, 0x0000B98B, 0x0CB27659),//EC_Trace

    /* Parameter Q
    Q:            B640000002A3A6F1D603AB4FF58EC74521F2934B1A7AEEDBE56F9B27E351457D
    nConst_Q:    2F2EE42B
    R mod Q:    49BFFFFFFD5C590E29FC54B00A7138BADE0D6CB4E58511241A9064D81CAEBA83
    R^2 mod Q:    2EA795A656F62FBDE479B522D6706E7B88F8105FAE1A5D3F27DEA312B417E2D2                            */
    BN256IN(0xB6400000, 0x02A3A6F1, 0xD603AB4F, 0xF58EC745, 0x21F2934B, 0x1A7AEEDB, 0xE56F9B27, 0xE351457D),//EC_Q
    0x2F2EE42B,                                                                                                //Q_Mc
    BN256IN(0x49BFFFFF, 0xFD5C590E, 0x29FC54B0, 0x0A7138BA, 0xDE0D6CB4, 0xE5851124, 0x1A9064D8, 0x1CAEBA83),//Q_R1        !!unused
    BN256IN(0x2EA795A6, 0x56F62FBD, 0xE479B522, 0xD6706E7B, 0x88F8105F, 0xAE1A5D3F, 0x27DEA312, 0xB417E2D2),//Q_R2

    /* Parameter N
    Q:            B640000002A3A6F1D603AB4FF58EC74449F2934B18EA8BEEE56EE19CD69ECF25
    nConst_Q:    51974B53
    R mod Q:    49BFFFFFFD5C590E29FC54B00A7138BBB60D6CB4E71574111A911E63296130DB
    R^2 mod Q:    8894F5D163695D0EBFEE4BAE7D78A1F9E4A08110BB6DAEAB7598CD79CD750C35                            */
    BN256IN(0xB6400000, 0x02A3A6F1, 0xD603AB4F, 0xF58EC744, 0x49F2934B, 0x18EA8BEE, 0xE56EE19C, 0xD69ECF25),//EC_N
    0x51974B53,                                                                                                //N_Mc
    BN256IN(0x776B0A2E, 0x9C96A2F1, 0x4011B451, 0x82875E06, 0x1B5F7EEF, 0x44925154, 0x8A673286, 0x328AF3CB),//N_R1        !!unused
    BN256IN(0x8894F5D1, 0x63695D0E, 0xBFEE4BAE, 0x7D78A1F9, 0xE4A08110, 0xBB6DAEAB, 0x7598CD79, 0xCD750C35),//N_R2

    /* Parameter #E(Fp)
    a *r mod Q:    0000000000000000000000000000000000000000000000000000000000000000
    b *r mod Q:    043FFFFFED866F6325E650D049188D1C125DF8F246A377FCB9F2C1E8C8C71995
    Gx*r mod Q:    5129787C869140B5EFD0CEC817A649BEA946FD5E0073282C22E935E29860501B
    Gy*r mod Q:    7215717763C39828326353912824EFBF15563CBDEC30A576EE779649EB87F7C7                            */
    BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000),//EC_Fp_A_Mont
    BN256IN(0x043FFFFF, 0xED866F63, 0x25E650D0, 0x49188D1C, 0x125DF8F2, 0x46A377FC, 0xB9F2C1E8, 0xC8C71995),//EC_Fp_B_Mont
    {//EC_Fp_G_Mont (SM9_ECP_A struct)
        BN256IN(0x5129787C, 0x869140B5, 0xEFD0CEC8, 0x17A649BE, 0xA946FD5E, 0x0073282C, 0x22E935E2, 0x9860501B),//EC_Fp_Gx_Mont
        BN256IN(0x72157177, 0x63C39828, 0x32635391, 0x2824EFBF, 0x15563CBD, 0xEC30A576, 0xEE779649, 0xEB87F7C7) //EC_Fp_Gy_Mont
    },

    /* Parameter #E(Fp^2)
    a *r mod Q:    0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000
    b *r mod Q:    0000000000000000000000000000000000000000000000000000000000000000, 043FFFFFED866F6325E650D049188D1C125DF8F246A377FCB9F2C1E8C8C71995
    Gx*r mod Q:    61FCF018BC47C4D1F8F57C82B14954447EE5645EDBF6C06B260226A68CE2DA8F, 905112F2B85F3A371874032F88791D4184C6135A5121F134DB6DB4822750A8A6
    Gy*r mod Q:    108495E0C0F62ECE2445561E2FF77CDB92FBAB45A15A3CA7C03F138F9171C24A, 81E448C3C76A5D531E29DE93D3EEF7693706F3F6A49DC12FF7B82DAC4C89BFBB    */
    {//EC_Fp2_A_Mont (SM9_Fp2 struct)
        BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000),//Coef_0
        BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000) //Coef_1
    },
    {//EC_Fp2_B_Mont (SM9_Fp2 struct)
        BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000),//Coef_0
        BN256IN(0x043FFFFF, 0xED866F63, 0x25E650D0, 0x49188D1C, 0x125DF8F2, 0x46A377FC, 0xB9F2C1E8, 0xC8C71995) //Coef_1
    },
    {//EC_Fp2_G_Mont (SM9_ECP2_A struct)
        {//X (SM9_Fp2)
            BN256IN(0x61FCF018, 0xBC47C4D1, 0xF8F57C82, 0xB1495444, 0x7EE5645E, 0xDBF6C06B, 0x260226A6, 0x8CE2DA8F),//Coef_0
            BN256IN(0x905112F2, 0xB85F3A37, 0x1874032F, 0x88791D41, 0x84C6135A, 0x5121F134, 0xDB6DB482, 0x2750A8A6) //Coef_1
        },
        {//Y (SM9_Fp2)
            BN256IN(0x108495E0, 0xC0F62ECE, 0x2445561E, 0x2FF77CDB, 0x92FBAB45, 0xA15A3CA7, 0xC03F138F, 0x9171C24A),//Coef_0
            BN256IN(0x81E448C3, 0xC76A5D53, 0x1E29DE93, 0xD3EEF769, 0x3706F3F6, 0xA49DC12F, 0xF7B82DAC, 0x4C89BFBB) //Coef_1
        }
    },

    /* Other Parameters
    v ^ q:        9848EEC25498CAB5B8554AB054AC91E3DB043BF50858278239B4EF0F3EE72529
    w ^ q:        9EF74015D5A16393F51F5EAC13DF846C9EC8547B245C54FD1A98DFBD4575299F
    (w^2) ^ q:    1C753E748601C9929C705DB2FD91512A08296B3557ED0186B626197DCE4736CA
    1/w^2:        3640000002A3A6F1D603AB4FF58EC74521F2934B1A7AEEDBE56F9B27E351457D
    one:        0000000000000000000000000000000000000000000000000000000000000001
    4^-1:        4000000000000000000000000000000000000000000000000000000000000000                            */
    BN256IN(0x9848EEC2, 0x5498CAB5, 0xB8554AB0, 0x54AC91E3, 0xDB043BF5, 0x08582782, 0x39B4EF0F, 0x3EE72529),//EC_vq_Mont
    BN256IN(0x9EF74015, 0xD5A16393, 0xF51F5EAC, 0x13DF846C, 0x9EC8547B, 0x245C54FD, 0x1A98DFBD, 0x4575299F),//EC_wq_Mont
    BN256IN(0x1C753E74, 0x8601C992, 0x9C705DB2, 0xFD91512A, 0x08296B35, 0x57ED0186, 0xB626197D, 0xCE4736CA),//EC_w2q_Mont
    BN256IN(0x36400000, 0x02A3A6F1, 0xD603AB4F, 0xF58EC745, 0x21F2934B, 0x1A7AEEDB, 0xE56F9B27, 0xE351457D),//EC_Root_Mont
    BN256IN(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001),//EC_One
    BN256IN(0x40000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000),//EC_4_Inv_Mont
};

unsigned char g_SM9_G1[64] = {
    0x93, 0xDE, 0x05, 0x1D, 0x62, 0xBF, 0x71, 0x8F, 0xF5, 0xED, 0x07, 0x04, 0x48, 0x7D, 0x01, 0xD6,
    0xE1, 0xE4, 0x08, 0x69, 0x09, 0xDC, 0x32, 0x80, 0xE8, 0xC4, 0xE4, 0x81, 0x7C, 0x66, 0xDD, 0xDD,
    0x21, 0xFE, 0x8D, 0xDA, 0x4F, 0x21, 0xE6, 0x07, 0x63, 0x10, 0x65, 0x12, 0x5C, 0x39, 0x5B, 0xBC,
    0x1C, 0x1C, 0x00, 0xCB, 0xFA, 0x60, 0x24, 0x35, 0x0C, 0x46, 0x4C, 0xD7, 0x0A, 0x3E, 0xA6, 0x16
};

unsigned char g_SM9_G2[128] = {
    0x85, 0xAE, 0xF3, 0xD0, 0x78, 0x64, 0x0C, 0x98, 0x59, 0x7B, 0x60, 0x27, 0xB4, 0x41, 0xA0, 0x1F,
    0xF1, 0xDD, 0x2C, 0x19, 0x0F, 0x5E, 0x93, 0xC4, 0x54, 0x80, 0x6C, 0x11, 0xD8, 0x80, 0x61, 0x41,
    0x37, 0x22, 0x75, 0x52, 0x92, 0x13, 0x0B, 0x08, 0xD2, 0xAA, 0xB9, 0x7F, 0xD3, 0x4E, 0xC1, 0x20,
    0xEE, 0x26, 0x59, 0x48, 0xD1, 0x9C, 0x17, 0xAB, 0xF9, 0xB7, 0x21, 0x3B, 0xAF, 0x82, 0xD6, 0x5B,
    0x17, 0x50, 0x9B, 0x09, 0x2E, 0x84, 0x5C, 0x12, 0x66, 0xBA, 0x0D, 0x26, 0x2C, 0xBE, 0xE6, 0xED,
    0x07, 0x36, 0xA9, 0x6F, 0xA3, 0x47, 0xC8, 0xBD, 0x85, 0x6D, 0xC7, 0x6B, 0x84, 0xEB, 0xEB, 0x96,
    0xA7, 0xCF, 0x28, 0xD5, 0x19, 0xBE, 0x3D, 0xA6, 0x5F, 0x31, 0x70, 0x15, 0x3D, 0x27, 0x8F, 0xF2,
    0x47, 0xEF, 0xBA, 0x98, 0xA7, 0x1A, 0x08, 0x11, 0x62, 0x15, 0xBB, 0xA5, 0xC9, 0x99, 0xA7, 0xC7
};

#endif // HITLS_CRYPTO_SM9
